package cn.tedu;

import java.sql.*;
import java.util.Scanner;

public class Demo09 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名");
        String username=sc.nextLine();
        System.out.println("请输入密码");
        String password=sc.nextLine();
        System.out.println("请输入昵称");
        String nick=sc.nextLine();
        try (Connection conn=DBUtils.getConn()){
            //先判断用户名是否存在
            String sql="select id from user where username=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            //替换 ?
            ps.setString(1,username);
            //执行查询
            ResultSet rs=ps.executeQuery();
            if (rs.next()){
                System.out.println("该用户已存在!");
                return;
            }else {
                String insertSql = "insert into user values(null,?,?,?)";
                PreparedStatement insertPs=conn.prepareStatement(insertSql);
                insertPs.setString(1,username);
                insertPs.setString(2,password);
                insertPs.setString(3,nick);
                insertPs.executeUpdate();
                System.out.println("注册成功!");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
